import { defineStore } from 'pinia'

export const useTagsViewStore = defineStore('tagsView', {
  state: () => ({
    tags: [{ path: '/dashboard', meta: { title: '首页' } }]
  }),
  actions: {
    addTag(route) {
      let path = typeof route.path === 'string' ? route.path : String(route.path)
      if (!/^\/[\w\-\/]+$/.test(path)) return
      if (!this.tags.find(tag => tag.path === path)) {
        this.tags.push({ path, meta: { ...route.meta } })
      }
    },
    removeTag(path) {
      this.tags = this.tags.filter(tag => tag.path !== path)
    },
    removeOthers(path) {
      this.tags = this.tags.filter(tag => tag.path === '/dashboard' || tag.path === path)
    },
    removeAll() {
      this.tags = this.tags.filter(tag => tag.path === '/dashboard')
    },
    removeLeft(tag) {
      const idx = this.tags.findIndex(t => t.path === tag.path)
      this.tags = this.tags.filter((t, i) => i >= idx || t.path === '/dashboard')
    },
    removeRight(tag) {
      const idx = this.tags.findIndex(t => t.path === tag.path)
      this.tags = this.tags.filter((t, i) => i <= idx || t.path === '/dashboard')
    }
  }
}) 